<?php
/** 大话水浒
 * Enter description here ...
 * @author Administrator
 *
 */
class Module_overlordcommodity{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	/**
	 * 物品介绍
	 * Enter description here ...
	 */
	function propintroduce(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$lv = isset($_GET['lv']) ? trim($_GET['lv']) : '';
		$smarty = loadSmarty();
		if (empty($type)){
			return View::error('参数错误.');
		}
		//物品配置类
		loadLib('App_Overlord_Collocate');
		//材料建筑配置
		$data = App_Overlord_Collocate::getGoods();
		//技能信息
		$skills = App_Overlord_Collocate::getSkill();
		if ($type != 'skills'){
			if (empty($data[$type][$id])){
				return View::error('没有此类型介绍.');
			}
		}else{
			$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
			$skills_type = isset($_GET['skills_type']) ? trim($_GET['skills_type']) : '';
			
			if (empty($skills[$skills_type][$type_id][$id])){
				return View::error('该本技能不存在.');
			}else{
				$smarty->assign('list',$skills[$skills_type][$type_id][$id]);
			}
		}
		//获取装备信息
		$equip = App_Overlord_Collocate::getEquipNature();
		if ($type == 'weapon' || $type == 'armour' || $type == 'helm' || $type == 'horse'){
		
			$equip_lv = $data[$type][$id]['lv'];
			//获取一级不同装备的数值
			if ($type == 'weapon'){
				if ($data[$type][$id]['num'] == 6){
					$equip_lv = $data[$type][$id]['lv'] . '_1';
				}elseif ($data[$type][$id]['num'] == 12){
					$equip_lv = $data[$type][$id]['lv'] . '_2';
				} 
			}
			if ($type == 'armour'){
				if ($data[$type][$id]['num'] == 4){
					$equip_lv = $data[$type][$id]['lv'] . '_1';
				}elseif ($data[$type][$id]['num'] == 7){
					$equip_lv = $data[$type][$id]['lv'] . '_2';
				} 
			}
			if ($type == 'helm'){
				if ($data[$type][$id]['num'] == 12){
					$equip_lv = $data[$type][$id]['lv'] . '_1';
				}elseif ($data[$type][$id]['num'] == 25){
					$equip_lv = $data[$type][$id]['lv'] . '_2';
				} 
			}
			if ($type == 'horse'){
				if ($data[$type][$id]['num'] == 40){
					$equip_lv = $data[$type][$id]['lv'] . '_1';
				}elseif ($data[$type][$id]['num'] == 80){
					$equip_lv = $data[$type][$id]['lv'] . '_2';
				} 
			}
			if (empty($equip[$type][$equip_lv][$lv])){
				return View::error('数据异常.');
			}
			$smarty->assign('num',$equip[$type][$equip_lv][$lv]);
			$smarty->assign('lv',$lv);
		}
		if (!empty($data[$type][$id])){
			$smarty->assign('list',$data[$type][$id]);
		}
		//print_r($data[$type][$id]);
		$smarty->assign('id',$id);
		$smarty->assign('type',$type);
		return $smarty->fetch('app/Overlord/prop_introduce.tpl.php');
	}
	/**
	 * 背包
	 * Enter description here ...
	 */
	function knapsack(){
		$top_type = isset($_GET['type']) && in_array($_GET['type'] , array('equip' , 'prop' , 'material' , 'treasure' , 'rest')) ? trim($_GET['type']) : 'equip';
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		if($page <= 0){
			$page = 1;
		}
		$count = 0;
		//控制显示数据数量
		$per_page = 10;
		/*******************数据分页******************/
		$offset = ($page-1) * $per_page;

		//查询君主信息
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主信息失败.');	
		}	
		//物品配置类
		loadLib('App_Overlord_Collocate');
		//装备配置
		$data = App_Overlord_Collocate::getGoods();
		//获取道具类型
		$type = App_Overlord_Collocate::getPropType();
		if ($top_type == 'equip'){
			if (!empty($items_info['Value'][$top_type])){
				foreach ($items_info['Value'][$top_type] as $k => $row){
					foreach ($row as $j => $rs){
						$items_info['Value'][$top_type][$k][$j]['name'] = $data[$k][$rs['id']]['name'];					
						if ($rs['state'] == 0){	
							//计数
							$count += 1;
						}
					}
				}
			}
		}elseif ($top_type == 'prop'){
			if (!empty($type)){
				foreach ($type as $k => $row){
					if ($row != 'material' && $row != 'key' && $row != 'treasure'){
						if (!empty($items_info['Value'][$row])){
							foreach ($items_info['Value'][$row] as $j => $rs){
								if (!empty($rs['count'])){	
									$items_info['Value'][$row][$j]['name'] = $data[$row][$j]['name'];
									//计数
									$count += 1;
								}
							}
							$newest[] = $row;
						}
					}
				}
			}
		
			if (!empty($newest)){
				$smarty->assign('type',$newest);
			}
		}elseif ($top_type == 'material'){
			//材料计数
			$material_count = 0;
			if (!empty($items_info['Value']['material'])){
				foreach ($items_info['Value']['material'] as $k => $row){
					$items_info['Value']['material'][$k]['name'] = $data['material'][$k]['name'];
					$material_count += $row['count'];
					if (!empty($row['count'])){	
						//计数
						$count += 1;
					}
				}
				if (!empty($material_count)){
					$smarty->assign('material_count',$material_count);
				}
			}
		}elseif ($top_type == 'treasure'){
			if (!empty($type)){
				foreach ($type as $k => $row){
					if ($row == 'key' || $row == 'treasure'){
						if (!empty($items_info['Value'][$row])){
							foreach ($items_info['Value'][$row] as $j => $rs){
								if (!empty($rs['count'])){	
									$items_info['Value'][$row][$j]['name'] = $data[$row][$j]['name'];
									//计数
									$count += 1;
								}
							}
							$newest[] = $row;
						}
					}
				}
				if (!empty($newest)){
					$smarty->assign('type',$newest);
				}
			}
		}elseif ($top_type == 'rest'){
			//技能书
			$skills = App_Overlord_Collocate::getSkill();
			if (!empty($items_info['Value']['skills'])){
				foreach ($items_info['Value']['skills'] as $k => $row){
					foreach ($row as $j => $rs){
						foreach ($rs as $g => $t){
							if (!empty($t['count'])){
								if (!empty($skills[$k][$j][$g]['name'])){
									$skills_info[$k][$j][$g]['name'] = $skills[$k][$j][$g]['name'];
									$skills_info[$k][$j][$g]['count'] = $t['count'];
									$count += 1;
								}
							}
						}
					}
				}
			}
	
			if (!empty($skills_info)){
				$smarty->assign('skills_info',$skills_info);
			}
			
		}
		if (!empty($items_info['Value'])){
			$smarty->assign('list',$items_info['Value']);
		}
	
		$smarty->assign('user_info',$user_info['Value']);
		$smarty->assign('top_type',$top_type);
		$smarty->assign('per_page',$per_page);
		$smarty->assign('offset',$offset);
		$smarty->assign('count',$count);
		return $smarty->fetch('app/Overlord/knapsack.tpl.php');
	}
	/**
	 * 商城
	 */
	function mall(){
		$top_type = isset($_GET['type']) && in_array($_GET['type'] , array('gold' , 'exploits' , 'diamond')) ? trim($_GET['type']) : 'diamond';
		$article_type = isset($_GET['article_type']) && in_array($_GET['article_type'] , array('weapon' , 'armour' , 'helm', 'horse', 'rest')) ? trim($_GET['article_type']) : 'weapon';
		
		$uin = SessionInfo('uin');
		$smarty = loadSmarty();
		
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		//查询君主信息
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主信息失败.');	
		}	
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info)){
			$items_info['Value'] = array();
			App_Overlord_Query::addItems($uin , $items_info['Value']);
		}
		loadLib('App_Overlord_Collocate');
		$data = App_Overlord_Collocate::getGoods();
		//获取道具类型
		$type = App_Overlord_Collocate::getPropType();
		$smarty->assign('top_type',$top_type);
		$smarty->assign('article_type',$article_type);
		$smarty->assign('list',$data);
		$smarty->assign('type',$type);
		$smarty->assign('user_info',$user_info['Value']);
		return $smarty->fetch('app/Overlord/mall.tpl.php');
	}
	/**
	 * 购买
	 * Enter description here ...
	 */
	function shopping(){
		$top_type = isset($_POST['type']) ? trim($_POST['type']) : '';  //货币
		$article_type = isset($_POST['article_type']) ? trim($_POST['article_type']) : ''; //类型
		$id = isset($_POST['id']) ? intval($_POST['id']) : null; //物品ID
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$num = isset($_POST['num']) ? intval($_POST['num']) : 1; //数量
		
		if ($top_type == 'gold'){
			$type_name = '黄金';
		}elseif ($top_type == 'exploits'){
			$type_name = '功勋';
		}elseif ($top_type == 'diamond'){
			$type_name = '钻石';
		}
		// ============================= 判断post传过来的值是否正确=====================
		if (empty($top_type) || empty($article_type)) {
			return View::error('参数错误.');
		}
		$uin = SessionInfo('uin');
		
		loadLib('App_Overlord_GameStart');
		//加载物品配置
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		//查询配置信息
		$data = App_Overlord_Collocate::getGoods();
		if (empty($data[$article_type][$id])){
			return View::error('商店没有此物品.');
		}
		if (empty($data[$article_type][$id][$top_type])){
			return View::error('数据异常.');
		}
		//查询君主信息
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('获取君主信息失败.');	
		}
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		//判断类型
		if ($article_type == 'weapon' || $article_type == 'armour' || $article_type == 'helm' || $article_type == 'horse'){
			//数据写入 
			$items_info['Value']['equip'][$article_type][$time] = array('id' => $id, 'lv' => 0, 'state' => 0, 'upgrade' => 0, 'repair' => 100);
			 $consume = $data[$article_type][$id][$top_type];
		}else{
			$consume = $data[$article_type][$id][$top_type] * $num;
			if (empty($items_info['Value'][$article_type][$id])){
				$items_info['Value'][$article_type][$id] = array('count' => $num);
			}else{
				$items_info['Value'][$article_type][$id]['count'] += $num;
			}
			
		}
		//购买花费
		$user_info['Value'][$top_type] -= $consume;
		if ($user_info['Value'][$top_type] < 0){
			if ($top_type == 'diamond'){
				return View::error('你的' . $type_name .'不足,不能购买' , 0 , array(
						array('name' => '去充值' , 'url' => '/app.php?m=overlorduser&amp;a=pay&amp;sid=' . sessionInfo('skey')),
					));
			}elseif ($top_type == 'gold'){
				return View::error('你的' . $type_name .'不足,不能购买<br />收取主城收益可获得黄金<br />' , 0 , array(
						array('name' => '去收取' , 'url' => '/app.php?m=overlordcity&amp;a=citygold&amp;sid=' . sessionInfo('skey')),
					));
			}else{
				return View::error('你的' . $type_name .'不足,不能购买<br />战斗胜利可获得功勋<br />' , 0 , array(
						array('name' => '去攻城' , 'url' => '/app.php?m=overlordcity&amp;a=battlecity&amp;sid=' . sessionInfo('skey')),
					));
			}
		}
		if (empty($items_info)){
			App_Overlord_Query::addItems($uin , $items_info['Value']);
		}else{
			App_Overlord_Query::alterItems($uin, $items_info['Value']);
		}
		if ($top_type != 'gold'){
			//道具的名字
			$name = $data[$article_type][$id]['name'];
			$information['name'] = '购买【'.$name.'】 x'. $num .',花费了'.$consume.$type_name;
			$information['time'] = $time;
			//消费记录
			App_Overlord_GameStart::getConsume($information);
		}
		//更新君主信息
		App_Overlord_Query::alterUser($uin, $user_info['Value']);
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=shop&top_type='.$id.'&num='.$num.'&article_type='.$article_type.'&id='.$top_type);
	}
/**
	 * 使用道具页面
	 */
	function propusage(){
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$id = isset($_POST['id']) ? trim($_POST['id']) : '';
		$url = isset($_POST['url']) ? trim($_POST['url']) : ''; //使用侦查术时记录URL出发点
		
		$uin = SessionInfo('uin');

		$to_uin = isset($_POST['to_uin']) ? trim($_POST['to_uin']) : ''; //使用侦查术获取信息
		if (empty($type)){
			return View::error('参数错误.');
		}
		// ================================ 侦查术 =====================================
		if (!empty($to_uin) && $to_uin == $uin){
			//传过来的to_uin 和自己的 uin 相同，直接传递
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=attribute&id='.$id);
			break;
		}

		$smarty = loadSmarty();
		//物品配置类
		loadLib('App_Overlord_Collocate');
		//获取道具配置
		$data = App_Overlord_Collocate::getGoods();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$prop = App_Overlord_Query::oneItems($uin);
	
		if (!empty($prop['Value'][$type])){
			if ($type == 'skills'){
				//技能书配置
				$skills = App_Overlord_Collocate::getSkill();
				foreach ($prop['Value'][$type] as $k => $row){
					foreach ($row as $j => $rs){
						foreach ($rs as $g => $t){
							if (!empty($t['count'])){
								$skills_info[$k][$j][$g]['name'] = $skills[$k][$j][$g]['name'];
								$skills_info[$k][$j][$g]['count'] = $t['count'];
							}
						}
					}
				}
			}elseif ($type == 'treasure'){
				//找出钥匙
				if (!empty($prop['Value']['key'][$id]['count'])){
					$list[$id]['name'] = $data['key'][$id]['name'];
					$list[$id]['count'] = $prop['Value']['key'][$id]['count'];
				}
			}else{
				//招募令牌
				if ($type == 'recruit'){
					$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
					//下次刷新时间
					$strtotime = App_Overlord_GameStart::getStrtotime();
					$cache_info = App_Overlord_Query::oneCollocate('app_overlord_generals_cache' , 'cache_' .$uin);
					/************************CD完刷新点将台***********************/
					if($time >= $cache_info['Value']['time']){
						App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
						break;
					}
				}
				foreach ($prop['Value'][$type] as $k => $row){
					if (!empty($row['count'])){
						$list[$k]['name'] = $data[$type][$k]['name'];
						$list[$k]['count'] = $row['count'];
					}
				}
			}
		}
	
		if (!empty($list)){
			$smarty->assign('list',$list);
		}
		if (!empty($skills_info)){
			$smarty->assign('skills',$skills_info);
		}
		if ($type == 'training'){
			$state_info = App_Overlord_Query::oneState($uin);
			if (empty($state_info)){
				return  View::error('数据异常!');
			}
			$user_info = App_Overlord_Query::oneUser($uin);
			if (empty($user_info)){
				return View::error('君主ID不存在.');
			}
			//查询兵种
			$barrackstype = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
			foreach ($barrackstype as $k => $row){
				if (!empty($row[$id])){
					$barracks_type = $row ;
				}
			}
			if (empty($barracks_type)){
				return View::error('没有此类型兵种.');
			}
			if (empty($state_info['Value']['train_barracks'][$id])){
				return View::error('数据异常!');
			}
			$barracks_type['time'] = App_Overlord_GameStart::getEndDate($state_info['Value']['train_barracks'][$id]['time']);
			if (!empty($barracks_type)){
				$smarty->assign('barracks',$barracks_type);
			}
		}
		$smarty->assign('url',$url);
		$smarty->assign('type',$type);
		$smarty->assign('id',$id);
		$smarty->assign('to_uin',$to_uin);
		return $smarty->fetch('app/Overlord/propusage.tpl.php');
	}
	/**
	 * 使用道具
	 * Enter description here ...
	 */
	function prop(){
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$type_id = isset($_POST['type_id']) ? trim($_POST['type_id']) : '';
		$id = isset($_POST['id']) ? trim($_POST['id']) : '';    //兵营训练士兵的ID,或者武将id
		$to_uin = isset($_POST['to_uin']) ? trim($_POST['to_uin']) : '';  //使用侦查术时的ID
		$url = isset($_POST['url']) ? trim($_POST['url']) : '';  //使用侦查术时的ID
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		// ======================= 对POST传值进行验证 =======================
		if (empty($type)) {
			return View::error('参数错误.');
		}
		$uin = SessionInfo('uin');
		// ============================ 侦查术 ================================
		if (!empty($to_uin) && $to_uin == $uin){
			//传过来的to_uin 和自己的 uin 相同，直接传递
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=attribute&id='.$id);
			break;
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		// ======================== 获取道具背包信息  ================================
		$prop = App_Overlord_Query::oneItems($uin);
		//技能学习
		if ($type == 'skills'){
			$skills_type = isset($_POST['skills_type']) ? trim($_POST['skills_type']) : '';
			$skills_id = isset($_POST['skills_id']) ? trim($_POST['skills_id']) : '';
			if (empty($prop['Value'][$type][$skills_type][$skills_id][$type_id]['count'])){
				return View::error('你还没有该书籍.');
			}
			//使用一本书籍
			$prop['Value'][$type][$skills_type][$skills_id][$type_id]['count'] -=  1;
		}else{
			if (empty($prop['Value'][$type][$type_id]['count'])){
				return View::error('你还没有该道具.');
			}
			//使用一个道具
			$prop['Value'][$type][$type_id]['count'] -= 1;
		}
	
		//物品配置类
		loadLib('App_Overlord_Collocate');
		//获取道具配置
		$data = App_Overlord_Collocate::getGoods();
		if ($type == 'skills'){
			//技能书配置
			$skills = App_Overlord_Collocate::getSkill();
			
			if (empty($skills[$skills_type][$skills_id])){
				return View::error('数据异常.');
			}
		}else{
			if (empty($data[$type][$type_id])){
				return View::error('数据异常.');
			}
			//道具详细信息
			$data_info = $data[$type][$type_id];
		}
		//获取任务类型配置
		loadLib('App_Overlord_Task');
		$task_type = App_Overlord_Task::getTaskType();
		
		if ($type == 'training'){
			// ======================== 获取兵营状态信息  ================================
			$state_brr = App_Overlord_Query::oneState($uin);
			
			if (empty($state_brr['Value']['train_barracks'][$id])){
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks');
				break;
			} 
			
			//使用减少招募士兵时间道具
			$state_brr['Value']['train_barracks'][$id]['time'] -= $data_info['num'] * 3600;
			App_Overlord_Query::alterState($uin , $state_brr['Value']);
			
			//完成加速任务
			App_Overlord_Task::getGo('main' , $task_type['main']['quicken']);
			//跳转练兵页面
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=training&type=training&id='.$id.'&type_id='.$type_id);
		}elseif ($type == 'check'){ //道具侦查术
			// 查询武将信息表
			$generals = App_Overlord_Query::oneUin_Generals($to_uin);
		
			if (empty($generals['Value'][$id])){
				return View::error('该武将不存在.');
			}
			//完成知己知彼任务
			App_Overlord_Task::getGo('main' , $task_type['main']['know']);
			//跳转武将页面
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=attribute&to_uin='.$to_uin.'&id='.$id .'&url=' .$url);
		}elseif ($type == 'expedition'){ //出征令
			// 查询可出征列表
			$fight = App_Overlord_Query::oneCollocate('app_overlord_fight_count' , 'fight_' .$uin);
			if (empty($fight)){
				$is_value = array('count' => 20, 'time' => $time);
				$fight['Value'] = $is_value;
				//初始化战斗次数
				$code = App_Overlord_Query::addCollocate('app_overlord_fight_count', 'fight_' .$uin, $is_value);
			}
			//获取时间
			$now_time = App_Overlord_GameStart::getTime();
			//判断是否已是第二天
			if($fight['Value']['time'] < $now_time[0]){
				$fight['Value']['time'] = $time;
				$fight['Value']['count'] = 20;
			}
			$fight['Value']['count'] += $data_info['num'];
			App_Overlord_Query::alterCollocate('app_overlord_fight_count', 'fight_' .$uin , $fight['Value']);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=prop&id='.$type.'&type_id='.$type_id);
		}elseif ($type == 'recruit'){
			//数据库武将招募列表
			$cache_info = App_Overlord_Query::oneCollocate('app_overlord_generals_cache' , 'cache_' .$uin);
			//君主信息
			$user_info = App_Overlord_Query::oneUser($uin);
			if (empty($user_info)){
				return View::error('获取君主数据失败.');
			}
			$generals = App_Overlord_Query::allCollocate('app_overlord_generals');
					
			//找出名将
			foreach ($generals as $k => $row){
			//名将
				$row['Value']['id'] = $k;
				if ($row['Value']['state'] == 1){
					//高级招募，几率本国名将
					
					$generals_state[] = $row;
					
				}else{
					//普通武将
					$generals_common[] = $row;
				}
			}
			$odds = rand(0 , 99);
			//产生几率
			if($odds >= $data_info['num']) {
			    //没有名将
			    $common_num = 3;
			    $state_num = 0;
			} else {
				// 名将数量
			    $state_num = 1;
			    $odds_s = rand(0 , 99); //再次几率
			    if($odds == 0 ){
			    	if ($odds_s <= 4){
			    		$state_num = 3;  // 4%的几率，三个名将
			    	}
			    }elseif($odds <= 1){ 
			    	if ($odds_s <= 10){
			    		$state_num = 2;   // 4%的几率，2个名将
			    	}
			    }
			    $common_num = 3 - $state_num;
			}
	
			// 根据 $common_num 和 $state_num 来获取将领
			if (!empty($common_num)){
				$common_count = count($generals_common) -1;
				//取普通将
				for ($i = 0; $i < $common_num; $i++){
					$common_rand = rand(0, $common_count);
					$key_rand['generals'][] = $generals[$generals_common[$common_rand]['Value']['id']]['Key']; 
				}
			}
			if (!empty($state_num)){
				$state_count = count($generals_state) -1;
				//取名将
				for ($i = 0; $i < $state_num; $i++){
					$state_rand = rand(0, $state_count);
					$key_rand['generals'][] = $generals[$generals_state[$state_rand]['Value']['id']]['Key']; 
				}
			}
			if (empty($cache_info)){
				$key_rand['time'] = $time;
			}else{
				$key_rand['time'] = $cache_info['Value']['time'];
			}
			$key_rand['to_time'] = 5 * 60; 

			$code = App_Overlord_Query::alterCollocate('app_overlord_generals_cache', 'cache_' .$uin, $key_rand);
			//跳转刷新武将页面
			if ($code == 0){
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordlnn&a=lnn');
			}
		}elseif ($type == 'cease'){
			//君主信息
			$user_info = App_Overlord_Query::oneUser($uin);
			if (empty($user_info)){
				return View::error('获取君主数据失败.');
			}
			//判断是否以前使用过休战牌
			if (empty($user_info['Value']['cease']) || $user_info['Value']['cease'] - $time < 0){
				$cease_time = $time;
			}else{
				$cease_time = $user_info['Value']['cease'];
			}
			//休战时间
			$user_info['Value']['cease'] = $cease_time + $data_info['num'] * 3600;
			
			App_Overlord_Query::alterUser($uin, $user_info['Value']);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=prop&id='.$type.'&type_id='.$type_id);
		}elseif ($type == 'recover'){ //补充精力界面
			//查询武将
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
			if (empty($generals_info)){
				return View::error('你还没有武将.');
			}
			if (empty($generals_info['Value'][$id])){
				return View::error('没有该武将信息.');
			}
			if ($generals_info['Value'][$id]['physical'] == 10){
				return View::error('你的武将不需要补充精力.');
			}
			//加体力，体力都是满
			$generals_info['Value'][$id]['physical'] = 10;
			//给武将恢复体力时间归0
			$generals_info['Value'][$id]['physical_time'] = 0;
			//修改武将
			App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);
			if ($url == 'generals'){		
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=attribute&id='.$id .'&url=' . $url);
			}elseif ($url == 'registration'){
				App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcolosseum&a=dispatch');
			}
		}elseif ($type == 'skills'){
			//查询武将
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
			if (empty($generals_info)){
				return View::error('你还没有武将.');
			}
			if (empty($generals_info['Value'][$id])){
				return View::error('没有该武将信息.');
			}
			if ($generals_info['Value'][$id]['state'] == 1){
				return View::error('你的武将出征中,无法学习技能.');
			}
			if ($generals_info['Value'][$id]['num'] <= 0){
				return View::error('你的技能已经是满了，无法再学习.');
			}	
			if (!empty($generals_info['Value'][$id][$type][$skills_type])){
				foreach ($generals_info['Value'][$id][$type][$skills_type] as $k => $row){
					//武技已学
					if ($k == $skills_id){
						return View::error('你的武将已经学习该技能.');
						break;
					}
				}
			}
		
			if (!empty($generals_info['Value'][$id][$type])){
				//存一个数组秘籍
				$generals_info['Value'][$id][$type][$skills_type][$skills_id] = $type_id;
			}else{
				//存一个数组秘籍
				$array[$skills_type][$skills_id] = $type_id;
				$generals_info['Value'][$id][$type] = $array;
			}
			//技能孔
			$generals_info['Value'][$id]['num'] -= 1; 
			//修改武将
			App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);		
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordskills&a=generalsskills&id='.$id);
		}elseif ($type == 'treasure'){
			if (empty($prop['Value']['key'][$type_id]['count'])){
				return View::error('你还没有钥匙.');
			}
			//使用一条钥匙
			$prop['Value']['key'][$type_id]['count'] -= 1;
			
			$treasure = App_Overlord_Collocate::getTreasure($type_id);
			if (empty($treasure)){
				return View::error('数据异常.');
			}
			loadLib('App_Overlord_Task');
			$task_type = App_Overlord_Task::getTaskType();
			if ($type_id == 0){
				//完成开铁宝箱任务
				App_Overlord_Task::getGo('main' , $task_type['main']['treasure']);
			}elseif ($type_id == 3){
				//完成开金宝箱任务
				App_Overlord_Task::getGo('main' , $task_type['main']['treasure_1']);
			}
			//种类随机
			$treasure_rand = array_rand($treasure);
			
			//分类随机
			$rand_type = array_rand($treasure[$treasure_rand]);
			
			//子类随机
			$prop_type = array_rand($treasure[$treasure_rand][$rand_type]);
			
			//物品的ID
			$prop_id = $treasure[$treasure_rand][$rand_type][$prop_type][0];
			//物品数量
			$prop_count = $treasure[$treasure_rand][$rand_type][$prop_type][1];

			//随机出来的是技能
			if ($treasure_rand == 'skills'){
				if (empty($prop['Value'][$treasure_rand][$rand_type][$prop_type][$prop_id])){
					$prop['Value'][$treasure_rand][$rand_type][$prop_type][$prop_id]['count'] = $prop_count;
				}else{
					$prop['Value'][$treasure_rand][$rand_type][$prop_type][$prop_id]['count'] += $prop_count;
				}
			}elseif ($treasure_rand == 'prop'){
			//随机出来的是道具
				if (empty($prop['Value'][$rand_type][$prop_id])){
					$prop['Value'][$rand_type][$prop_id] = array('count' => $prop_count);
				}else{
					$prop['Value'][$rand_type][$prop_id]['count'] += $prop_count;
				}
			}elseif ($treasure_rand == 'equip'){
			//随机出来的是装备
				$prop['Value'][$treasure_rand][$rand_type][$time] = array('id' => $prop_id, 'lv' => 0, 'state' => 0, 'upgrade' => 0, 'repair' => 100);
			}
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordcommodity&a=treasure&id='.$prop_count.'&treasure_rand='.$treasure_rand.'&rand_type='.$rand_type.'&prop_id='.$prop_id.'&prop_type='.$prop_type);
		}elseif ($type == 'wipe_off'){
			//使用遗忘武将技能道具
			//查询武将
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
			
			if (empty($generals_info)){
				return View::error('你还没有武将.');
			}
			if (empty($generals_info['Value'][$id])){
				return View::error('没有该武将信息.');
			}
			if ($generals_info['Value'][$id]['state'] == 1){
				return View::error('你的武将出征中,无法清除技能.');
			}
			if (empty($generals_info['Value'][$id]['skills'])){
				return View::error('你的武将还没有学习技能.');
			}
			//查询数据库武将
			$data_generals_info = App_Overlord_Query::oneCollocate('app_overlord_generals' , $generals_info['Value'][$id]['generals']);
			if (empty($data_generals_info)){
				return View::error('武将库不存在该武将信息.');
			}
			//如果有天生技能、去天生技能的类型和ID
			if (!empty($data_generals_info['Value']['skills'])){
				foreach ($data_generals_info['Value']['skills'] as $data_k => $data_row){
					foreach ($data_row as $data_j => $data_rs){
							
					}
				}
			}
			//初始技能孔
			$num = 0;
			//循环判断是否学习武技,出天生技能
			foreach ($generals_info['Value'][$id]['skills'] as $k => $row){
				foreach ($row as $j => $rs){
					//判断该武将是否有天生技能
					if (!empty($data_j)){
						if ($j != $data_j){
							$skills_rand_type[$k][$j] = $rs;
							//计数技能孔
							$num += 1;
						}
					}else{
						//无天生技能
						$skills_rand_type[$k][$j] = $rs;
						//计数技能孔
						$num += 1;
					} 
				}
			}
	//	print_r($skills_rand_type);	
			//除了天生只能外没有学习其他技能
			if (!empty($data_generals_info['Value']['skills']) && empty($skills_rand_type)){
				return View::error('你的武将还没学习技能.');
			}
			//初始化随机类型，方便传值
			$rand_type = 0;
			$rand_skills = 0;
			$rand_skills_num = 0;
			
			$generals_row = $generals_info['Value'][$id];
			loadLib('App_Overlord_Collocate');
			//技能加载
			$skill = App_Overlord_Collocate::getSkill();
			//查询武将可带兵数
			$is_count = App_Overlord_GameStart::getGeneralsBarracksLv($generals_row['lv']);
			//武器攻击初始
			$grow_ap_num = 0;
			if (!empty($generals_row['equipment']['weapon']['num'])){
				$grow_ap_num = $generals_row['equipment']['weapon']['num'];
			}
			//盔甲防御初始
			$grow_defense_num = 0;
			if (!empty($generals_row['equipment']['armour']['num'])){
				$grow_defense_num = $generals_row['equipment']['armour']['num'];
			}
			//头盔
			$grow_hp_num = 0;
			if (!empty($generals_row['equipment']['helm']['num'])){
				$grow_hp_num = $generals_row['equipment']['helm']['num'];
			}
			//初始化武将带兵数
			$make_count = $is_count['count'];
			//初始化攻击
			$grow_ap = $generals_info['Value'][$id]['ap'] - $grow_ap_num;
			//初始化防御力
			$grow_defense = $generals_info['Value'][$id]['defense'] - $grow_defense_num;
			//初始化生命力
			$grow_hp = $generals_info['Value'][$id]['hp'] - $grow_hp_num;
			if ($type_id == 1){	
				if (!empty($generals_row['skills']['strengthen'])){
					//如果有加兵技能
					foreach ($generals_row['skills']['strengthen'] as $k => $skills_row){
						if ($k == 'staff'){
							//带兵加成
							$skills_num = $skill['strengthen'][$k][$skills_row]['num'];
							$make_count -= $make_count * $skills_num/100;
						}
					}
				}
				if (!empty($generals_row['skills']['study'])){
					//如果有攻击加成技能
					foreach ($generals_row['skills']['study'] as $k => $skills_row){
						if ($k == 'ap'){
							//攻击力加成
							$skills_num = $skill['study'][$k][$skills_row]['num'];
							$grow_ap -= $grow_ap * $skills_num/100;
							$grow_ap += $grow_ap_num;
							$generals_info['Value'][$id]['ap'] = $grow_ap;
						}elseif ($k == 'defense'){
							//防御力加成
							$skills_num = $skill['study'][$k][$skills_row]['num'];
							$grow_defense -= $grow_defense * $skills_num/100;
							$grow_defense += $grow_defense_num;
							$generals_info['Value'][$id]['defense'] = $grow_defense;
						}elseif ($k == 'hp'){
							//生命力加成
							$skills_num = $skill['study'][$k][$skills_row]['num'];
							$grow_hp -= $grow_hp * $skills_num/100;
							$grow_hp += $grow_hp_num;
							$generals_info['Value'][$id]['hp'] = $grow_hp;
						}
					}
				}
				
				//使用重修之书,武将技能清空
				$generals_info['Value'][$id]['skills'] = '';
				//重新赋值技能孔
				$generals_info['Value'][$id]['num'] += $num;
				//如果该武将有天生技能
				if (!empty($data_generals_info['Value']['skills'])){
					//重新赋值武将天生技能
					$generals_info['Value'][$id]['skills'] = $data_generals_info['Value']['skills'];
				}
			}elseif ($type_id == 0){
				//使用遗忘之书,武将技能随机去掉一个技能，天生技能除外
				if (!empty($skills_rand_type)){
					//随机技能类型
					$rand_type = array_rand($skills_rand_type);
					//随机技能子类
					$rand_skills = array_rand($skills_rand_type[$rand_type]);
					//技能级别
					$rand_skills_num = $skills_rand_type[$rand_type][$rand_skills];
					//循环干掉一个技能,除出天生技能
					foreach ($generals_info['Value'][$id]['skills'] as $k => $row){
						foreach ($row as $j => $rs){
							if ($j != $rand_skills){
								//重新赋值
								$skills_info[$k][$j] = $rs;
							}
						}
					}
				if (!empty($generals_row['skills']['strengthen'])){
					//如果有加兵技能
					foreach ($generals_row['skills']['strengthen'] as $k => $skills_row){
						if ($k == 'staff' && $k == $rand_skills){
							//带兵加成
							$skills_num = $skill['strengthen'][$k][$skills_row]['num'];
							$make_count -= $make_count * $skills_num/100;
						}
					}
				}
					if (!empty($generals_row['skills']['study'])){
						//如果有攻击加成技能
						foreach ($generals_row['skills']['study'] as $k => $skills_row){
							if ($k == $rand_skills){
								if ($k == 'ap'){
									//攻击力加成
									$skills_num = $skill['study'][$k][$skills_row]['num'];
									$grow_ap -= $grow_ap * $skills_num/100;
									$grow_ap += $grow_ap_num;
									$generals_info['Value'][$id]['ap'] = $grow_ap;
								}elseif ($k == 'defense'){
									//防御力加成
									$skills_num = $skill['study'][$k][$skills_row]['num'];
									$grow_defense -= $grow_defense * $skills_num/100;
									$grow_defense += $grow_defense_num;
									$generals_info['Value'][$id]['defense'] = $grow_defense;
								}elseif ($k == 'hp'){
									//生命力加成
									$skills_num = $skill['study'][$k][$skills_row]['num'];
									$grow_hp -= $grow_hp * $skills_num/100;
									$grow_hp += $grow_hp_num;
									$generals_info['Value'][$id]['hp'] = $grow_hp;
								}
							}
						}
					}
					//武将技能清空
					$generals_info['Value'][$id]['skills'] = '';
					//重新赋值技能孔
					$generals_info['Value'][$id]['num'] += 1;
					if (!empty($skills_info)){
						//重新赋值最新技能
						$generals_info['Value'][$id]['skills'] = $skills_info;
					}
				}
			}else{
				return View::error('数据异常.');
			}
			//战马
			if (!empty($generals_row['equipment']['horse']['num'])){
				$make_count += $generals_row['equipment']['horse']['num'];
				$generals_info['Value'][$id]['make_count'] = $make_count;
			}

			//修改武将
			$code = App_Overlord_Query::alterUin_Generals($uin , $generals_info['Value']);		

			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=success&type=wipe_off&id='.$id.'&type_id='.$type_id.'&rand_type='.$rand_type.'&rand_skills='.$rand_skills.'&rand_skills_num='.$rand_skills_num);
		}else{
			return View::error('数据异常.');
		}
		
		//修改道具信息
		App_Overlord_Query::alterItems($uin , $prop['Value']);
	}
	/**
	 * 宝箱开启获得的物品
	 * Enter description here ...
	 */
	function treasure(){
		$treasure_rand = isset($_GET['treasure_rand']) ? trim($_GET['treasure_rand']) : '';
		$rand_type = isset($_GET['rand_type']) ? trim($_GET['rand_type']) : '';
		$prop_id = isset($_GET['prop_id']) ? trim($_GET['prop_id']) : '';
		$prop_type = isset($_GET['prop_type']) ? trim($_GET['prop_type']) : '';
		$id = isset($_GET['id']) ? intval($_GET['id']) : 1;
		
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_Query');
		//查询配置物品信息
		$data = App_Overlord_Collocate::getGoods();
		//技能
		$skill = App_Overlord_Collocate::getSkill();
		if ($treasure_rand == 'skills'){
			if (!empty($skill[$rand_type][$prop_type][$prop_id]['name'])){
				$name = $skill[$rand_type][$prop_type][$prop_id]['name'];
			}else{
				return View::error('数据异常.!');
			}
		}else{
			if (!empty($data[$rand_type][$prop_id]['name'])){
				$name = $data[$rand_type][$prop_id]['name'];
			}else{
				return View::error('数据异常.!');
			}
		}
	/*	return App_Overlord_GameStart::success('你获得了<br />'.$name .' x'.$id,array(
				array('name' => '返回我的背包' , 'url' => '/app.php?m=overlordcommodity&amp;a=knapsack&amp;sid=' . sessionInfo('skey')),
				));*/
		
		loadLib('App_Overlord_GameStart');
		//查询背包的内容
		$items_info = App_Overlord_Query::oneItems($uin);
		if (empty($items_info)){
			return View::error('数据异常!');
		}
		//获取道具类型
		$type = App_Overlord_Collocate::getPropType();
		if (!empty($type)){
			foreach ($type as $k => $row){
				if ($row == 'treasure'){
					if (!empty($items_info['Value'][$row])){
						foreach ($items_info['Value'][$row] as $j => $rs){
							$items_info['Value'][$row][$j]['name'] = $data[$row][$j]['name'];
						}
					}
				}
			}
		}
		
		$smarty = loadSmarty();
		if (!empty($items_info['Value']['treasure'])){
			$smarty->assign('list' , $items_info['Value']['treasure']);
		}
		$smarty->assign('name' , $name);
		$smarty->assign('id' , $id);
		return $smarty->fetch('app/Overlord/treasure.tpl.php');
	}
}
# end of script